package com.hllxd.hism.star.basic.service.service;


import com.hllxd.hism.star.basic.service.dao.mapper.doris.*;
import com.hllxd.hism.star.basic.service.model.doris.*;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

/**
 * 消费报告
 *
 * @author lixiyong
 * @date 2020/12/20 16:15
 */
@Slf4j
@Service("detailService")
public class DetailServiceImpl implements DetailService {

    @Autowired
    private ActualInfodetailMapper actualInfodetailMapper;

    @Autowired
    private BillingDetailMapper billingDetailMapper;

    @Autowired
    private ExposureDetailMapper exposureDetailMapper;

    @Autowired
    private ResumedeliveryDetailMapper resumedeliveryDetailMapper;

    @Autowired
    private ImOpenMapper imOpenMapper;

    @Autowired
    private IminviteMapper iminviteMapper;

    @Autowired
    private JobPublishMapper jobPublishMapper;

    @Autowired
    private B2cBillMapper b2cBillMapper;

    @Autowired
    private AxbbillInfoMapper axbbillInfoMapper;


    @Override
    public DetailEntity<ActualInfodetail> listClickDetail(String startTime, String endTime) {
        HashMap<String, String> map = new HashMap<>();
        if ("".equals(startTime) || startTime == null) {
            map.put("startTime", getYesterday(0)+" "+getCurrentHour()+":00:00");
        } else {
            map.put("startTime", startTime);
        }

        if ("".equals(endTime) || endTime == null) {
            map.put("endTime", getYesterday(0)+" "+getCurrentHourM());
        } else {
            map.put("endTime", endTime);
        }

        map.put("yesDate", LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        List<ActualInfodetail> details = actualInfodetailMapper.selectByEtime(map);
        DetailEntity<ActualInfodetail> entity = new DetailEntity<>();
        entity.setStartTime(map.get("startTime"));
        entity.setEndTime(map.get("endTime"));
        entity.setDataList(details);
        return entity;
    }

    @Override
    public DetailEntity<RealexposureEntity> listExposureDetail(String startTime, String endTime) {
        HashMap<String, String> map = new HashMap<>();
        if ("".equals(startTime) || startTime == null) {
            map.put("startTime", getYesterday(0)+" "+getCurrentHour()+":00:00");
        } else {
            map.put("startTime", startTime);
        }

        if ("".equals(endTime) || endTime == null) {
            map.put("endTime", getYesterday(0)+" "+getCurrentHourM());
        } else {
            map.put("endTime", endTime);
        }

        map.put("yesDate", LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        List<RealexposureEntity> details = exposureDetailMapper.selectByEtime(map);
        DetailEntity<RealexposureEntity> entity = new DetailEntity<>();
        entity.setStartTime(map.get("startTime"));
        entity.setEndTime(map.get("endTime"));
        entity.setDataList(details);
        return entity;
    }

    @Override
    public DetailEntity<ResumedeliveryEntity> listResumedeliveryDetail(String startTime, String endTime) {
        HashMap<String, String> map = new HashMap<>();
        if ("".equals(startTime) || startTime == null) {
            map.put("startTime", getYesterday(0)+" "+getCurrentHour()+":00:00");
        } else {
            map.put("startTime", startTime);
        }

        if ("".equals(endTime) || endTime == null) {
            map.put("endTime", getYesterday(0)+" "+getCurrentHourM());
        } else {
            map.put("endTime", endTime);
        }
        map.put("yesDate", LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        List<ResumedeliveryEntity> details = resumedeliveryDetailMapper.selectByEtime(map);
        DetailEntity<ResumedeliveryEntity> entity = new DetailEntity<>();
        entity.setStartTime(map.get("startTime"));
        entity.setEndTime(map.get("endTime"));
        entity.setDataList(details);
        return entity;
    }

    @Override
    public DetailEntity<BillingDetailEntity> listBillingDetail(String startTime, String endTime) {
        HashMap<String, String> map = new HashMap<>();
        if ("".equals(startTime) || startTime == null) {
            map.put("startTime", getYesterday(0)+" "+getCurrentHour()+":00:00");
        } else {
            map.put("startTime", startTime);
        }

        if ("".equals(endTime) || endTime == null) {
            map.put("endTime", getYesterday(0)+" "+getCurrentHourM());
        } else {
            map.put("endTime", endTime);
        }
        List<BillingDetailEntity> details = billingDetailMapper.selectByEtime(map);
        DetailEntity<BillingDetailEntity> entity = new DetailEntity<>();
        entity.setStartTime(map.get("startTime"));
        entity.setEndTime(map.get("endTime"));
        entity.setDataList(details);
        return entity;
    }

    @Override
    public DetailEntity<ImOpenEntity> listImOpenDetail(String startTime, String endTime) {
        HashMap<String, String> map = new HashMap<>();
        if ("".equals(startTime) || startTime == null) {
            map.put("startTime", getYesterday(0)+" "+getCurrentHour()+":00:00");
        } else {
            map.put("startTime", startTime);
        }

        if ("".equals(endTime) || endTime == null) {
            map.put("endTime", getYesterday(0)+" "+getCurrentHourM());
        } else {
            map.put("endTime", endTime);
        }
        map.put("yesDate", LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        List<ImOpenEntity> details = imOpenMapper.selectByEtime(map);
        DetailEntity<ImOpenEntity> entity = new DetailEntity<>();
        entity.setStartTime(map.get("startTime"));
        entity.setEndTime(map.get("endTime"));
        entity.setDataList(details);
        return entity;
    }

    @Override
    public DetailEntity<IminviteEntity> listIminviteDetail(String startTime, String endTime) {
        HashMap<String, String> map = new HashMap<>();
        if ("".equals(startTime) || startTime == null) {
            map.put("startTime", getYesterday(0)+" "+getCurrentHour()+":00:00");
        } else {
            map.put("startTime", startTime);
        }

        if ("".equals(endTime) || endTime == null) {
            map.put("endTime", getYesterday(0)+" "+getCurrentHourM());
        } else {
            map.put("endTime", endTime);
        }
        List<IminviteEntity> details = iminviteMapper.selectByEtime(map);
        DetailEntity<IminviteEntity> entity = new DetailEntity<>();
        entity.setStartTime(map.get("startTime"));
        entity.setEndTime(map.get("endTime"));
        entity.setDataList(details);
        return entity;
    }

    @Override
    public DetailEntity<JobPublishEntity> listJobPublishDetail(String startTime, String endTime) {
        HashMap<String, String> map = new HashMap<>();
        if ("".equals(startTime) || startTime == null) {
            map.put("startTime", getYesterday(0)+" "+getCurrentHour()+":00:00");
        } else {
            map.put("startTime", startTime);
        }

        if ("".equals(endTime) || endTime == null) {
            map.put("endTime", getYesterday(0)+" "+getCurrentHourM());
        } else {
            map.put("endTime", endTime);
        }
        List<JobPublishEntity> details = jobPublishMapper.selectByEtime(map);
        DetailEntity<JobPublishEntity> entity = new DetailEntity<>();
        entity.setStartTime(map.get("startTime"));
        entity.setEndTime(map.get("endTime"));
        entity.setDataList(details);
        return entity;
    }

    @Override
    public DetailEntity<B2cBillEntity> listB2cBillDetail(String startTime, String endTime) {
        HashMap<String, String> map = new HashMap<>();
        if ("".equals(startTime) || startTime == null) {
            map.put("startTime", getYesterday(0)+" "+getCurrentHour()+":00:00");
        } else {
            map.put("startTime", startTime);
        }

        if ("".equals(endTime) || endTime == null) {
            map.put("endTime", getYesterday(0)+" "+getCurrentHourM());
        } else {
            map.put("endTime", endTime);
        }
        List<B2cBillEntity> details = b2cBillMapper.selectByEtime(map);
        DetailEntity<B2cBillEntity> entity = new DetailEntity<>();
        entity.setStartTime(map.get("startTime"));
        entity.setEndTime(map.get("endTime"));
        entity.setDataList(details);
        return entity;
    }

    @Override
    public DetailEntity<AxbbillInfoEntity> listAxbbillInfoDetail(String startTime, String endTime) {
        HashMap<String, String> map = new HashMap<>();
        if ("".equals(startTime) || startTime == null) {
            map.put("startTime", getYesterday(0)+" "+getCurrentHour()+":00:00");
        } else {
            map.put("startTime", startTime);
        }

        if ("".equals(endTime) || endTime == null) {
            map.put("endTime", getYesterday(0)+" "+getCurrentHourM());
        } else {
            map.put("endTime", endTime);
        }
        List<AxbbillInfoEntity> details = axbbillInfoMapper.selectByEtime(map);
        DetailEntity<AxbbillInfoEntity> entity = new DetailEntity<>();
        entity.setStartTime(map.get("startTime"));
        entity.setEndTime(map.get("endTime"));
        entity.setDataList(details);
        return entity;
    }

    public String getYesterday(int t) {
        return LocalDate.now().minusDays(t).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }
    public String getCurrentHour() {
        DateTime now = DateTime.now();
        return String.format("%02d",now.getHourOfDay());
    }
    public String getCurrentHourM() {
        DateTime now = DateTime.now();
        return String.format("%02d:%02d:00",now.getHourOfDay(),now.getMinuteOfHour());
    }


}
